package com.quzi.kettle.datapiece;

import android.util.Log;

import com.quzi.kettle.datapiece.status.DeviceStatus;
import com.quzi.kettle.utils.ByteUtils;

public class DataBodyDevResponse extends FactoryDataBody {

	public DataBodyDevResponse() {
	}

	/**
	 * 第零个byte
	 */
	byte Zerobyte;

	/**
	 * 第一个byte
	 */
	byte FirstByte;
	boolean switchStatus;// 开关状态
	int runNingStatus;// 运行状态
	boolean waterPumb;// 水泵
	int timerMode;// 定时方式
	boolean test1; // 快检
	int connectMode;// 连接状态

	/**
	 * 第二个byte
	 */
	byte Secondbyte;

	int setTemperature;// 设定温度

	/**
	 * 第三个byte
	 */
	byte Thirdbyte;

	int setFixedTemperatureTime;// 设定保温
	boolean get_rid_cl_work;// 除氯状态
	/**
	 * 第四个byte
	 */
	byte Fourthbyte;
	int appointed_timer_on_aboso_min;// 预约开-绝对分
	boolean appointed_timer_on; // 预约开

	/**
	 * 第五个byte
	 */
	byte Fifthbyte;
	int appointed_timer_on_aboso_hour;// 预约开-绝对小时
	boolean fixed_timer_on;// 定时开

	/**
	 * 第六个byte
	 */
	byte Sixthbyte;
	int fixed_timer_on_aboso_min;// 定时开-绝对分
	boolean mobile_timer; // 移动终端定时信息

	/**
	 * 第七个byte ？
	 */
	byte Seventhbyte;
	int fixed_timer_on_aboso_hour;// 定时开-绝对小时

	/**
	 * 第八个byte
	 */
	byte Eighthbyte;
	boolean Saturday; // 周六
	boolean Friday; // 周五
	boolean Thursday; // 周四
	boolean Wednesday; // 周三
	boolean Tuesday; // 周二
	boolean Monday; // 周一
	boolean Sunday; // 周日
	boolean weekTimerLoop; // 周定时循环
	/**
	 * 第九个byte
	 */
	byte Ninethbyte;
	int pumbAddWater;// 水泵加水量
	boolean pumbCont; // 水泵控制
	boolean beeWarning;// 蜂鸣器提醒

	/**
	 * 第十个byte
	 */
	byte Tenthbyte;
	int lightCont; // 亮度控制
	int contWay; // 控制方式
	boolean lampCont; // 灯控制

	/**
	 * 第十一个byte
	 */
	byte Eleventhbyte;
	int boilingTimer;// 沸腾时间控制
	boolean get_rid_cl; // 除氯开关
	int whatmode;

	@Override
	public DeviceStatus toObject(byte[] srcbytes) {

		byte[] bytes = new byte[13];
		System.arraycopy(srcbytes, 5, bytes, 0, 12);
		// Log.d("mmqtt",""+ByteUtils.printHexString(bytes));

		Zerobyte = bytes[0];

		FirstByte = bytes[1];
		switchStatus = ((bytes[1] & 0x02) >> 1) == 1 ? true : false;
		runNingStatus = ((bytes[1] & 0x04) >> 2) == 1 ? 1 : 0;
		Log.d("datadev", "runNingStatus = " + runNingStatus + "   " + bytes[1]);
		waterPumb = ((bytes[1] & 0x08) >> 3) == 1 ? true : false;
		timerMode = ((bytes[1] & 0x10) >> 4) == 1 ? 1 : 0;
		test1 = ((bytes[1] & 0x20) >> 5) == 1 ? true : false;
		connectMode = ((bytes[1] & 0x40) >> 6) == 1 ? 1 : 0;

		Secondbyte = bytes[2];
		Log.d("temperature",
				"bytes[2] = " + bytes[2] + "   "
						+ ByteUtils.printHexString(bytes[2]));
		setTemperature = (byte) ((bytes[2] & 0xfe) >> 1);
		Log.d("temperature", "setTemp = " + setTemperature + "   byte2 = "
				+ bytes[2]);
		Thirdbyte = bytes[3];
		setFixedTemperatureTime = (int) (bytes[3] & 0x7f);
		get_rid_cl_work = ((bytes[3] & 0x80) >> 7) == 1 ? true : false;

		Fourthbyte = bytes[4];
		appointed_timer_on_aboso_min = (int) (bytes[4] & 0x3f);
		appointed_timer_on = ((bytes[4] & 0x40) >> 6) == 1 ? true : false;

		Fifthbyte = bytes[5];
		appointed_timer_on_aboso_hour = (int) ((bytes[5] & 0x3E) >> 1);
		fixed_timer_on = ((bytes[5] & 0x40) >> 6) == 1 ? true : false;

		Sixthbyte = bytes[6];
		fixed_timer_on_aboso_min = (int) (bytes[6] & 0x3F);
		mobile_timer = ((bytes[6] & 0x40) >> 6) == 1 ? true : false;

		Seventhbyte = bytes[7];
		fixed_timer_on_aboso_hour = (int) (bytes[7] & 0x3F);

		Eighthbyte = bytes[8];
		Saturday = (bytes[8] & 0x01) == 1 ? true : false;
		Friday = ((bytes[8] & 0x02) >> 1) == 1 ? true : false;
		Thursday = ((bytes[8] & 0x04) >> 2) == 1 ? true : false;
		Wednesday = ((bytes[8] & 0x08) >> 3) == 1 ? true : false;
		Tuesday = ((bytes[8] & 0x10) >> 4) == 1 ? true : false;
		Monday = ((bytes[8] & 0x20) >> 5) == 1 ? true : false;
		Sunday = ((bytes[8] & 0x40) >> 6) == 1 ? true : false;
		weekTimerLoop = ((bytes[8] & 0x80) >> 7) == 1 ? true : false;

		Ninethbyte = bytes[9];
		pumbAddWater = (int) ((bytes[9] & 0x1E) >> 1);
		pumbCont = ((bytes[9] & 0x20) >> 1) == 1 ? true : false;
		beeWarning = ((bytes[9] & 0x40) >> 2) == 1 ? true : false;

		Tenthbyte = bytes[10];
		lightCont = (int) ((bytes[10] & 0x1E) >> 1);
		contWay = ((bytes[10] & 0x20) >> 1) == 1 ? 1 : 0;
		lampCont = ((bytes[10] & 0x40) >> 2) == 1 ? true : false;

		Eleventhbyte = bytes[11];
		boilingTimer = (int) ((bytes[11] & 0x3E) >> 1);
		get_rid_cl = ((bytes[11] & 0x40) >> 6) == 1 ? true : false;
		whatmode = (int) (bytes[11] & 0xC0) >> 6;
		DeviceStatus currStatus = new DeviceStatus();
		currStatus = setCurrStatus(currStatus);

		return currStatus;
	}

	private DeviceStatus setCurrStatus(DeviceStatus currStatus) {
		// TODO Auto-generated method stub
		currStatus.switchStatus = this.switchStatus ? (byte) 0x01 : 0;
		currStatus.runNingStatus = (byte) this.runNingStatus;
		currStatus.waterPumb = this.waterPumb ? (byte) 0x01 : 0;
		currStatus.timerMode = (byte) this.timerMode;
		currStatus.test1 = (byte) (this.test1 ? 0x01 : 0);
		currStatus.connectMode = (byte) this.connectMode;

		currStatus.setTemperature = (byte) this.setTemperature;

		// currStatus.setFixedTemperatureTime = (byte)
		// this.setFixedTemperatureTime;
		// currStatus.get_rid_cl_work = this.get_rid_cl_work ? (byte) 0x01 : 0;

		currStatus.appointed_timer_on_aboso_min = (byte) this.appointed_timer_on_aboso_min;
		currStatus.appointed_timer_on = this.appointed_timer_on ? (byte) 0x01
				: 0;

		currStatus.appointed_timer_on_aboso_hour = (byte) this.appointed_timer_on_aboso_hour;
		currStatus.fixed_timer_on = this.fixed_timer_on ? (byte) 0x01 : 0;

		currStatus.fixed_timer_on_aboso_min = (byte) this.fixed_timer_on_aboso_min;
		currStatus.mobile_timer = this.mobile_timer ? (byte) 0x01 : 0;

		currStatus.fixed_timer_on_aboso_hour = (byte) this.fixed_timer_on_aboso_hour;

		currStatus.Saturday = this.Saturday ? (byte) 0x01 : 0;
		currStatus.Friday = this.Friday ? (byte) 0x01 : 0;
		currStatus.Thursday = this.Thursday ? (byte) 0x01 : 0;
		currStatus.Wednesday = this.Wednesday ? (byte) 0x01 : 0;
		currStatus.Tuesday = this.Tuesday ? (byte) 0x01 : 0;
		currStatus.Monday = this.Monday ? (byte) 0x01 : 0;
		currStatus.Sunday = this.Sunday ? (byte) 0x01 : 0;
		currStatus.weekTimerLoop = this.weekTimerLoop ? (byte) 0x01 : 0;

		currStatus.pumbAddWater = (byte) this.pumbAddWater;
		currStatus.pumbCont = this.pumbCont ? (byte) 0x01 : 0;
		currStatus.beeWarning = this.beeWarning ? (byte) 0x01 : 0;

		currStatus.lightCont = (byte) this.lightCont;
		currStatus.contWay = (byte) this.contWay;
		currStatus.lampCont = this.lampCont ? (byte) 0x01 : 0;

		currStatus.boilingTimer = (byte) this.boilingTimer;
		currStatus.get_rid_cl = this.get_rid_cl ? (byte) 0x01 : 0;
		currStatus.whatmode = (byte) this.whatmode;
		/* ....... */

		return currStatus;
	}

	// 相应位 置布尔值
	private boolean getTargetBit(byte b, byte target) {
		return (b & target) == 1 ? true : false;
	}

	private void antiBytes(byte[] bytes) {
		Zerobyte = bytes[0];
		FirstByte = bytes[1];
		Secondbyte = bytes[2];
		Thirdbyte = bytes[3];
		Fourthbyte = bytes[4];
		Fifthbyte = bytes[5];
		Sixthbyte = bytes[6];
		Seventhbyte = bytes[7];
		Eighthbyte = bytes[8];
		Ninethbyte = bytes[9];
		Tenthbyte = bytes[10];
		Eleventhbyte = bytes[11];

	}

	@Override
	public byte[] toBytes() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setDataBodyStatus(DeviceStatus newStatus) {
		// TODO Auto-generated method stub

	}
}
